package com.siao.data
{
	import com.siao.events.DataChangeEvent;
	
	import flash.events.EventDispatcher;
	
	[Event(name="change", type="com.siao.events.DataChangeEvent")]
	
	/**
	 * 数组列表对象
	 * @author SiaoLeon
	 */
	public class ArrayList extends EventDispatcher
	{
		// -------------------------------------------------------------------------------------------------------- 静态常量
		
		// -------------------------------------------------------------------------------------------------------- 静态变量
		
		// -------------------------------------------------------------------------------------------------------- 静态方法
		
		// -------------------------------------------------------------------------------------------------------- 静态函数
		
		// -------------------------------------------------------------------------------------------------------- 属性
		
		/**
		 * 列表长度
		 * @return
		 */
		public function get length():uint
		{
			return this.list.length;
		}
		
		// -------------------------------------------------------------------------------------------------------- 变量
		
		private var list:Array;
		
		// -------------------------------------------------------------------------------------------------------- 构造
		
		/**
		 * 构造函数
		 */
		public function ArrayList(... elements)
		{
			this.list = elements;
		}
		
		// -------------------------------------------------------------------------------------------------------- 方法
		
		/**
		 * 列表与数组连接
		 * @param array 数组对象
		 * @return 若数组对象为null，则返回列表对象的复制副本
		 */
		public function concat(array:Array=null):ArrayList
		{
			var result:ArrayList = new ArrayList();
			result.list = this.list.concat();
			
			if (array != null)
			{
				for (var i:int = 0; i < array.length; i++)
				{
					result.list.push(array[i]);
				}
			}
			
			return result;
		}
		
		/**
		 * 替换为另一个数组列表的数据
		 * @param array 数组列表对象
		 */
		public function replace(array:ArrayList):void
		{
			this.list.splice(0, this.list.length);
			
			for (var i:int = 0; i < array.length; i++)
			{
				this.list[i] = (array.element(i));
			}
			
			this.dispatchEvent(new DataChangeEvent("replace", array, this));
		}
		
		/**
		 * 添加一个元素
		 * @param element 元素对象
		 */
		public function add(element:*):void
		{
			this.list.push(element);
			
			this.dispatchEvent(new DataChangeEvent(this.length.toString(), null, element));
		}
		
		/**
		 * 在指定索引添加元素
		 * @param element 元素对象
		 * @param index 索引位置
		 */
		public function addAt(element:*, index:uint):void
		{
			this.list.splice(index, element);
			
			this.dispatchEvent(new DataChangeEvent(index.toString(), null, element));
		}
		
		/**
		 * 设置指定索引的元素
		 * @param index 索引位置
		 * @param element 元素对象
		 */
		public function setElement(index:uint, element:*):void
		{
			var oldElement:* = this.list[index];
			
			this.list[index] = element;
			
			this.dispatchEvent(new DataChangeEvent(index.toString(), oldElement, element));
		}
		
		/**
		 * 移除一个元素
		 * @param element 元素对象
		 * @return
		 */
		public function remove(element:*):uint
		{
			var index:int = this.indexOf(element);
			
			if (index >= 0)
			{
				this.removeAt(index);
			}
			
			return index;
		}
		
		/**
		 * 移除指定索引的元素
		 * @param index 索引位置
		 * @return
		 */
		public function removeAt(index:uint):*
		{
			if (index > this.length - 1)
			{
				return null;
			}
			
			var element:* = this.element(index);
			this.list.splice(index, 1);
			
			this.dispatchEvent(new DataChangeEvent(index.toString(), element, null));
			
			return element;
		}
		
		/**
		 * 清空列表
		 */
		public function clear():void
		{
			var len:uint = this.length;
			
			this.list.splice(0, this.list.length);
			
			this.dispatchEvent(new DataChangeEvent("length", len, 0));
		}
		
		/**
		 * 获取指定索引位置的元素
		 * @param index 索引位置
		 * @return
		 */
		public function element(index:uint):*
		{
			return this.list[index];
		}
		
		/**
		 * 获取元素的索引
		 * @param element 元素对象
		 * @return
		 */
		public function indexOf(element:*):int
		{
			return this.list.indexOf(element);
		}
		
		/**
		 * 检测列表中是否包含指定元素
		 * @param element 元素对象
		 * @return
		 */
		public function contains(element:*):Boolean
		{
			return this.indexOf(element) >= 0;
		}
		
		/**
		 * 返回列表的数组对象
		 * @return
		 */
		public function toArray():Array
		{
			return this.list.concat();
		}
		
		/**
		 * 排序
		 * @param compareFunction
		 */
		public function sort(compareFunction:Function):void
		{
			this.list.sort(compareFunction);
			
			this.dispatchEvent(new DataChangeEvent("sort", this, this));
		}
		
		/**
		 * 排序
		 * @param names
		 * @param options
		 */
		public function sortOn(names:*, options:*=0):void
		{
			this.list.sortOn(names, options);
			
			this.dispatchEvent(new DataChangeEvent("sort", this, this));
		}
		
		override public function toString():String
		{
			return String(this.list);
		}
	
		// -------------------------------------------------------------------------------------------------------- 函数
	
	}
}
